import { createServer, IncomingMessage, ServerResponse } from 'http';
import { parse } from 'url';
import next from 'next';

const dev = process.env.NODE_ENV !== 'production';
const app = next({ dev });
const handle = app.getRequestHandler();

// 准备Next.js应用
app.prepare().then(() => {
    // 创建HTTP服务器
    const server = createServer((req: IncomingMessage, res: ServerResponse) => {
        if (req.url) {
            const parsedUrl = parse(req.url, true);
            handle(req, res, parsedUrl);
        } else {
            res.writeHead(400);
            res.end('无效请求');
        }
    });

    // 启动服务器
    server.listen(3008, '0.0.0.0', () => {
        console.log('> Ready on http://localhost:3008');
        console.log('> Also available on your local network IP');
    });
}).catch((ex: Error) => {
    console.error(ex.stack);
    process.exit(1);
}); 